﻿using Asxsyd92Core.Utils.Provider;
using System;
using System.Linq;
using WebOS.Dal;
namespace WebOS.Bll
{
    public class DictionaryBll
    {
        public static DictionaryBll Instance
        {
            get { return SingletonProvider<DictionaryBll>.Instance; }
        }
        public string GetDictionary(long page, long limt)
        {
            var da = DictionaryDal.Instance.GetPage(page, limt, "ID", new { ParentID = Guid.Empty }).ToList();
            //return Asxsyd92Core.Utils.JSONhelper.ToJson(da);
            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = da.Count, data = da }, false);
        }
        public string GetDictionaryByID(Guid id)
        {
            var da = DictionaryDal.Instance.GetWhere(new { ParentID = id }).ToList();
            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = da.Count, data = da }, false);
        }
        public string GetOptionsByCode(string v, string value)
        {     object o = null;
            var da = DictionaryDal.Instance.GetWhere(new { Code = v }).FirstOrDefault();
            if (da != null)
            {
                var das = DictionaryDal.Instance.GetWhere(new { ParentID = da.ID }).ToList();
                var db = DBConnectionBll.Instance.getTables_SqlServer();
                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = das.Count == 0 ? 0 : das.First().Count, data = das, db = db }, false);
            }
       
            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = 0, data = o }, false);
        }

        public string Delte(Guid guid) {
           var tag= DictionaryDal.Instance.Delete(guid);
            DictionaryDal.Instance.Delete(new { ParentID= guid });
            if (tag > 0)
            {
                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { msg = "删除成功！", Success = true });
            }
            else {
                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { msg = "删除失败！", Success = false });
            }
      
        }
        /// <summary>
        /// 通过code获取字典项
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetByCode(string code)
        {
            object tag0 = null;

            tag0 = WebOS.Dal.DictionaryDal.Instance.GetList("select * from Dictionary where ParentID in (select ID from Dictionary where Code='" + code + "')", null);

            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { data = tag0, Success = true });


        }
    }
}
